﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace HtmlAgilityPackExample
{
    class Program
    {
        static StreamWriter s_writer;

        static void ParseTD(HtmlNode tdNode)
        {
            HtmlNode node = tdNode.FirstChild;
            while (null != node)
            {
                switch (node.Name)
                {
                    case "ul":
                        ParseUL(node);
                        break;
                    default:
                        s_writer.Write($"{node.InnerText},");
                        break;
                }

                node = node.NextSibling;
            }
        }

        static void ParseTR(HtmlNode trNode)
        {
            HtmlNode node = trNode.FirstChild;
            while (null != node)
            {
                switch (node.Name)
                {
                    case "td":
                        ParseTD(node);
                        break;
                }

                node = node.NextSibling;
            }

            s_writer.WriteLine();
        }

        static void ParseTable(HtmlNode tableNode)
        {
            s_writer.WriteLine();

            HtmlNode node = tableNode.FirstChild;
            while (null != node)
            {
                switch (node.Name)
                {
                    case "tr":
                        ParseTR(node);
                        break;
                }

                node = node.NextSibling;
            }
        }

        static void ParseUL(HtmlNode ulNode)
        {
            HtmlNode node = ulNode.FirstChild;
            while (null != node)
            {
                switch (node.Name)
                {
                    case "table":
                        ParseTable(node);
                        break;
                    case "b":
                        s_writer.WriteLine(node.InnerText);
                        break;
                    case "ul":
                        ParseUL(node);
                        break;
                    case "li":
                        ParseLI(node);
                        break;
                }

                node = node.NextSibling;
            }
        }

        static void ParseLI(HtmlNode liNode)
        {
            HtmlNode node = liNode.FirstChild;
            while (null != node)
            {
                switch (node.Name)
                {
                    case "span":
                        s_writer.WriteLine(node.InnerText);
                        break;
                    case "ul":
                        ParseUL(node);
                        break;
                    case "#text":
                        if (node.InnerText != "\n")
                            s_writer.WriteLine(node.InnerText);
                        break;
                }

                node = node.NextSibling;
            }
        }

        static void Main(string[] args)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.Load("./_10May2019_15h41m48s.html");

            /* 保存为CSV文件 */
            using (s_writer = new StreamWriter("./_10May2019_15h41m48s.csv", false, Encoding.UTF8))
            {
                HtmlNode hrNode = doc.DocumentNode.SelectSingleNode("//hr[1]");

                HtmlNode node = hrNode.NextSibling;
                while (null != node)
                {
                    //s_writer.WriteLine(node.Name);
                    switch (node.Name)
                    {
                        case "big":
                            s_writer.WriteLine(node.InnerText);
                            break;
                        case "br":
                            //s_writer.WriteLine();
                            break;
                        case "b":
                            s_writer.WriteLine(node.InnerText);
                            break;
                        case "hr":
                            s_writer.WriteLine();
                            s_writer.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                            break;
                        case "ul":
                            ParseUL(node);
                            break;
                        case "table":
                            ParseTable(node);
                            break;
                    }

                    node = node.NextSibling;
                }

                s_writer.Close();
            }

            Console.WriteLine("Done");

            Console.ReadLine();
        }
    }
}
